﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;
using PsychveyApp1.classes;

namespace PsychveyApp1
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            if (Request.Cookies["Username"] != null && Request.Cookies["Password"] != null)
            {
                //if (Request.Cookies["RememberMe"].Value=="true")
                //{
                tbxUsername.Text = Request.Cookies["Username"].Value;
                tbxPassword.Attributes["value"] = Request.Cookies["Password"].Value;
                chkRememberMe.Checked = true;
                //}
            }
            else {
                chkRememberMe.Checked = false;
            }
        }

        

        protected void btnUserSignin_Click(object sender, EventArgs e)
        {
            if (chkRememberMe.Checked)
            {
                Response.Cookies["RememberMe"].Value = "true";
                Response.Cookies["Username"].Expires = DateTime.Now.AddDays(30);
                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(30);
            }
            else
            {
                Response.Cookies["RememberMe"].Value = "false";
                Response.Cookies["Username"].Expires = DateTime.Now.AddDays(-1);
                Response.Cookies["Password"].Expires = DateTime.Now.AddDays(-1);

            }
            Response.Cookies["Username"].Value = tbxUsername.Text.Trim();
            Response.Cookies["Password"].Value = tbxPassword.Text;

            UserAccount useraccount = new UserAccount();
            string usertext = tbxUsername.Text.Trim();
            Session["username"] = usertext;
            string passtext = tbxPassword.Text;
            Session["password"] = passtext;

            useraccount = DBManager.UserLogin(usertext,passtext);


            if ((tbxUsername.Text.Equals("")) && (tbxPassword.Text.Equals("")))
            {
                lblStatusUser.Text = "Please type username and password";
            }
            else
            {
                if (DBManager.checkIfUserAccountExist(usertext,passtext) == 1)
                {
                    Session["currentUser"] = useraccount;
                    Response.Redirect("MySurvey.aspx");
                }
                else if(DBManager.checkIfUserAccountExist(usertext,passtext) == 0)
                {
                    lblStatusUser.Text = "Username and password does not match";
                }

            }
        }

        protected void btnAnonymousSignIn_Click(object sender, EventArgs e)
        {
            Response.Redirect("AnonymousAnswerSurveyHome.aspx");
        }
        }
    }
